home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6836 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  91 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!hester
  3. From: hester@netcom.com (Jim Hester)
  4. Subject: Function returning pointer to equivalent functions
  5. Message-ID: <hesterDn1E2r.K1F@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. Distribution: usa
  8. Date: Mon, 19 Feb 1996 18:59:15 GMT
  9. Sender: hester@netcom9.netcom.com
  10.  
  11.  
  12. I want a group of functions that each can return a pointer to any other
  13. function in that group.  I can't find any direct way to declare the
  14. function's type since the return type ends up in a "recursive" definition
  15. loop.  I'd prefer to avoid dirty type changes like using void pointers.
  16.  
  17. The best that I have done is to envelope the functions in a class and
  18. return a pointer to the class (example below).  Context: The purpose
  19. of these functions is to run an automaton.  The main() function below
  20. demonstrates it's intended control (except the input char, which SHOULD
  21. be pulled from an input stream).
  22.  
  23. If states are numbered (as in my unimaginative example below), it's easy
  24. to have the functions return indices in an array containing pointers to
  25. all of the functions.  That's the method I've used in the past.  The
  26. only problem is that I can't use descriptive state names without going
  27. into manually #defining constants for function locations.
  28.  
  29. My main question is:
  30. Can anyone show me a way of directly declaring the functions to return the
  31. pointer I want?
  32.  
  33. If not, other suggestions?
  34.  
  35. If not, can anybody improve what I have below?  The best improvement I
  36. can think of is a casting operator from FnEnvelope to pFnEnvelope that
  37. would let me forget about adding the '&'s where I need them below.
  38. I tried (in public):    operator StateType() {return this;}
  39. but the compiler didn't like it.
  40.  
  41. #include <iostream.h>
  42.  
  43. class FnEnvelope;
  44.  
  45. typedef StateType (*pStateFn)(char);
  46.  
  47. class FnEnvelope
  48. {
  49.     public:
  50.     FnEnvelope (StateType (*i_F)(char)) : F(i_F) { }
  51.     StateType Run(char c) { return (*F)(c); }
  52.  
  53.     private:
  54.     StateType (*F)(char);
  55. };
  56.  
  57. StateType State1Fn(char);    FnEnvelope State1(State1Fn);
  58. StateType State2Fn(char);    FnEnvelope State2(State2Fn);
  59. FnEnvelope StateQuit(NULL);
  60.  
  61. StateType State1Fn(char c)
  62. {
  63.     cout << "this is state1: " << c << endl; // testing
  64.     switch (c)
  65.     {
  66.     case 'e':
  67.         return &StateQuit;
  68.     default:
  69.         return &State2;
  70.     }
  71. }
  72.  
  73. StateType State2Fn(char c)
  74. {
  75.     cout << "this is state2: " << c << endl; // testing
  76.     switch (c)
  77.     {
  78.     case 'e':
  79.         return &StateQuit;
  80.     default:
  81.         return &State1;
  82.     }
  83. }
  84.  
  85. void main(void)
  86. {
  87.     StateType State;
  88.     char c = 'a';
  89.     for ( State = &State1 ; State != &StateQuit ; State = State->Run(c++) );
  90. }
  91.